home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / VetoableChangeSupport.java < prev    next >
Text File  |  1998-09-22  |  5KB  |  156 lines

  1. /*
  2.  * @(#)VetoableChangeSupport.java    1.14 98/07/01
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.beans;
  16.  
  17. import java.io.Serializable;
  18. import java.io.ObjectOutputStream;
  19. import java.io.ObjectInputStream;
  20. import java.io.IOException;
  21.  
  22.  
  23. /**
  24.  * This is a utility class that can be used by beans that support constrained
  25.  * properties.  You can use an instance of this class as a member field
  26.  * of your bean and delegate various work to it.
  27.  */
  28.  
  29. public class VetoableChangeSupport implements java.io.Serializable {
  30.  
  31.     /**
  32.      * @sourceBean  The bean to be given as the source for any events.
  33.      */
  34.  
  35.     public VetoableChangeSupport(Object sourceBean) {
  36.     source = sourceBean;
  37.     }
  38.  
  39.     /**
  40.      * Add a VetoableListener to the listener list.
  41.      *
  42.      * @param listener  The VetoableChangeListener to be added
  43.      */
  44.  
  45.     public synchronized void addVetoableChangeListener(
  46.                     VetoableChangeListener listener) {
  47.     if (listeners == null) {
  48.         listeners = new java.util.Vector();
  49.     }
  50.     listeners.addElement(listener);
  51.     }
  52.  
  53.     /**
  54.      * Remove a VetoableChangeListener from the listener list.
  55.      *
  56.      * @param listener  The VetoableChangeListener to be removed
  57.      */
  58.     public synchronized void removeVetoableChangeListener(
  59.                     VetoableChangeListener listener) {
  60.     if (listeners == null) {
  61.         return;
  62.     }
  63.     listeners.removeElement(listener);
  64.     }
  65.  
  66.     /**
  67.      * Report a vetoable property update to any registered listeners.  If
  68.      * anyone vetos the change, then fire a new event reverting everyone to 
  69.      * the old value and then rethrow the PropertyVetoException.
  70.      * <p>
  71.      * No event is fired if old and new are equal and non-null.
  72.      *
  73.      * @param propertyName  The programmatic name of the property
  74.      *        that was changed.
  75.      * @param oldValue  The old value of the property.
  76.      * @param newValue  The new value of the property.
  77.      * @exception PropertyVetoException if the recipient wishes the property
  78.      *              change to be rolled back.
  79.      */
  80.     public void fireVetoableChange(String propertyName, 
  81.                     Object oldValue, Object newValue)
  82.                     throws PropertyVetoException {
  83.  
  84.     if (oldValue != null && oldValue.equals(newValue)) {
  85.         return;
  86.     }
  87.  
  88.     java.util.Vector targets;
  89.     synchronized (this) {
  90.         if (listeners == null) {
  91.             return;
  92.         }
  93.         targets = (java.util.Vector) listeners.clone();
  94.     }
  95.         PropertyChangeEvent evt = new PropertyChangeEvent(source,
  96.                         propertyName, oldValue, newValue);
  97.  
  98.     try {
  99.         for (int i = 0; i < targets.size(); i++) {
  100.             VetoableChangeListener target = 
  101.                 (VetoableChangeListener)targets.elementAt(i);
  102.             target.vetoableChange(evt);
  103.         }
  104.     } catch (PropertyVetoException veto) {
  105.         // Create an event to revert everyone to the old value.
  106.                evt = new PropertyChangeEvent(source, propertyName, newValue, oldValue);
  107.         for (int i = 0; i < targets.size(); i++) {
  108.         try {
  109.                 VetoableChangeListener target =
  110.                 (VetoableChangeListener)targets.elementAt(i);
  111.                 target.vetoableChange(evt);
  112.         } catch (PropertyVetoException ex) {
  113.              // We just ignore exceptions that occur during reversions.
  114.         }
  115.         }
  116.         // And now rethrow the PropertyVetoException.
  117.         throw veto;
  118.     }
  119.     }
  120.  
  121.     private void writeObject(ObjectOutputStream s) throws IOException {
  122.         s.defaultWriteObject();
  123.  
  124.     java.util.Vector v = null;
  125.     synchronized (this) {
  126.         if (listeners != null) {
  127.             v = (java.util.Vector) listeners.clone();
  128.             }
  129.     }
  130.  
  131.     if (v != null) {
  132.         for(int i = 0; i < listeners.size(); i++) {
  133.             VetoableChangeListener l = (VetoableChangeListener)v.elementAt(i);
  134.             if (l instanceof Serializable) {
  135.                 s.writeObject(l);
  136.             }
  137.             }
  138.         }
  139.         s.writeObject(null);
  140.     }
  141.  
  142.  
  143.     private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
  144.         s.defaultReadObject();
  145.       
  146.         Object listenerOrNull;
  147.         while(null != (listenerOrNull = s.readObject())) {
  148.         addVetoableChangeListener((VetoableChangeListener)listenerOrNull);
  149.         }
  150.     }
  151.  
  152.     transient private java.util.Vector listeners;
  153.     private Object source;
  154.     private int vetoableChangeSupportSerializedDataVersion = 1;
  155. }
  156.